//
//  WebVC.m
//  xiaoyixiu
//
//  Created by hanzhanbing on 16/6/19.
//  Copyright © 2016年 柯南. All rights reserved.
//

/**
 *  网页展示通用页面
 *
 */

#import "WebVC.h"
//#import "WebviewProgressLine.h"
//#import "CircleModel.h"
//#import "NewCircleViewController.h"
//#import "EOFlashChatView.h"
@interface WebVC ()
//@property (nonatomic,strong) WebviewProgressLine  *progressLine;
@end

@implementation WebVC

- (UIWebView *)webView
{
    if (_webView == nil) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        _webView.dataDetectorTypes = UIDataDetectorTypeNone;
        _webView.scrollView.bounces=NO;
        _webView.delegate = self;
        
    }
    return _webView;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //
    self.view.backgroundColor=ColorWhite;
//    self.navigationController.navigationBar.barTintColor=ColorWhite;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
//    if (self.isYellow) {
//        [self.navigationController.navigationBar navBarBackGroundColor:kColorFromRGBHex(0x323232) image:nil isOpaque:YES];
//        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"saosaoBack"] style:UIBarButtonItemStyleDone target:self action:@selector(BackClick)];
//        UILabel *titlab=[UILabel CreatLabeltext:self.navTitle Font:kFONT(18) Textcolor:kColorFromRGBHex(0xffd700) textAlignment:NSTextAlignmentCenter];
//        titlab.frame=CGRectMake(0, 0, 100, 100);
//        self.navigationItem.titleView=titlab;
//    }else{
//        [self.navigationController.navigationBar navBarBackGroundColor:ColorWhite image:nil isOpaque:YES];
//        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回(2)"] style:UIBarButtonItemStyleDone target:self action:@selector(BackClick)];
        UILabel *titlab=[UILabel CreatLabeltext:self.navTitle Font:KMEDIUM(18) Textcolor:ColorMain textAlignment:NSTextAlignmentCenter];
        titlab.frame=CGRectMake(0, 0, 100, 100);
        self.navigationItem.titleView=titlab;
//    }
    
}
-(void)BackClick{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    //    self.navigationController.navigationBar.hidden=YES;
}
-(void)backAction
{
    
    if (self.webView.canGoBack==YES) {
        [self.webView goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.webView];
    self.view.backgroundColor=ColorWhite;
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.mas_offset(0);
        make.bottom.mas_offset(-BOTTOM_HEIGHT);
    }];
}



- (void)loadLocalFile:(NSString *)fileName
{
    NSArray * arr = [fileName componentsSeparatedByString:@","];
    NSString *path = [[NSBundle mainBundle] pathForResource:arr[0] ofType:arr[1] inDirectory:@"alwaysOx"];
    
    
    //文件内容string
    NSString *file = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
    
    NSString *fileString = [[NSString alloc] initWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
    [self.webView loadHTMLString:fileString baseURL:nil];
}

- (void)loadFromURLStr:(NSString *)urlStr
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr ]];
    [self.webView loadRequest:request];
    [self.webView setScalesPageToFit:YES];
    
}
- (void)loadFromURLHtml:(NSString *)html
{
    
    [self.webView loadHTMLString:html baseURL:nil];
    
    
}
#pragma mark -WebViewDelegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    
    if ([request.URL.absoluteString containsString:@"data:image/png;"]) {
        NSArray *array=[request.URL.absoluteString componentsSeparatedByString:@","];
        if (array.count>1) {
            NSData *decodedImageData = [[NSData alloc]
                                        
                                        initWithBase64EncodedString:array[1] options:NSDataBase64DecodingIgnoreUnknownCharacters];
            ;
            UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:decodedImageData], self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
            
        }
        return NO;
        
    }
    // click event
    if ([request.URL.host isEqualToString:@"call_close"])
    {// notify click close
        
        
        return NO;
    }
    
    if ([request.URL.host isEqualToString:@"on_click"])
    {
        NSString *URL_String = [request.URL.path substringFromIndex:1];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL_String]];
        
        
        return NO;
    }
    
    return YES;
}
//指定回调方法
-(void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{
   
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    [webView setScalesPageToFit:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MessageAlertView dismissHud];
    });
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MessageAlertView dismissHud];
    });
}



@end
